#include "Model.h"
#include <d3d9.h>
#include <d3dx9.h>

Model::Model(){
	
	mTransMatrix = new D3DXMATRIX();
	mScaleMatrix = new D3DXMATRIX();
	mRotXMatrix= new D3DXMATRIX();
	mRotYMatrix= new D3DXMATRIX();;
	mRotZMatrix= new D3DXMATRIX();;

	mScale.x = 1.0f;
	mScale.y = 1.0f;
	mScale.z = 1.0f;

	mRot.x = 0.0f;
	mRot.y = 0.0f;
	mRot.z = 0.0f;

	mPos.x = 0.0f;
	mPos.y = 0.0f;
	mPos.z = 0.0f;

 
	
	//_mDefault.x
	mMesh = new Mesh("Default","_mDefault.x");
}

Model::Model(string pName, string pTextureLoc){

	mName = pName;

	mTransMatrix = new D3DXMATRIX();
	mScaleMatrix = new D3DXMATRIX();
	mRotXMatrix= new D3DXMATRIX();
	mRotYMatrix= new D3DXMATRIX();;
	mRotZMatrix= new D3DXMATRIX();;

	mScale.x = 1.0f;
	mScale.y = 1.0f;
	mScale.z = 1.0f;

	mRot.x = 0.0f;
	mRot.y = 0.0f;
	mRot.z = 0.0f;

	mPos.x = 0.0f;
	mPos.y = 0.0f;
	mPos.z = 0.0f;


	//Set default material properties
 

	mMesh = new Mesh(pName,pTextureLoc);
}

void Model::LoadContent(LPDIRECT3DDEVICE9 d3ddev){

	mMesh->LoadMesh(d3ddev);
}

void Model::Update(LPDIRECT3DDEVICE9 d3ddev, double pElapsedTime){
	
	for(int a =0; a < mAnimations.size();a ++){
		mAnimations[a]->Update(d3ddev,pElapsedTime);
	}
}

Mesh* Model::GetMesh(){
	return mMesh;
}

string Model::GetName(){
	return mName;
}

D3DVECTOR* Model::GetPos(){
	return &mPos;
}

D3DVECTOR* Model::GetRot(){
	return &mRot;
}
D3DVECTOR* Model::GetScale(){
	return &mScale;
}


void Model::SetName(string pName){
	mName = pName;
}
void Model::SetMesh(Mesh* pMesh){
	mMesh = pMesh;
}
void Model::SetRot(D3DVECTOR* pRot){
	mRot = *(pRot);
}
void Model::SetScale(D3DVECTOR* pScale){
	mScale = *(pScale);
}
void Model::SetPos(D3DVECTOR* pPos){
	mPos = *(pPos);
}

//Returns a matrix with all the transformations for the object
D3DXMATRIX* Model::GetTransMatrix(){
	D3DXMatrixRotationX(mRotXMatrix,mRot.x);
	D3DXMatrixRotationY(mRotYMatrix,mRot.y);
	D3DXMatrixRotationZ(mRotZMatrix,mRot.z);
	D3DXMatrixScaling(mScaleMatrix,mScale.x,mScale.y,mScale.z);
	D3DXMatrixTranslation(mTransMatrix,mPos.x,mPos.y, mPos.z);
	
	return &( *(mRotXMatrix) * *(mRotYMatrix) * *(mRotZMatrix) * *(mScaleMatrix) * *(mTransMatrix));
}

void Model::Draw(LPDIRECT3DDEVICE9 d3ddev){
	D3DXMATRIX* mTransforms = GetTransMatrix();
	d3ddev->SetTransform(D3DTS_WORLD, mTransforms);
	//d3ddev->SetMaterial(&mMaterial);
	mMesh->Draw(d3ddev);
}


void Model::Release(){
	mMesh->Release();

}
void Model::AddAnimation(Animation* pAnim){
	mAnimations.push_back(pAnim);
}


